package org.example.ui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TerminateFrame extends JFrame implements ActionListener {

    private JButton exit;
    private JButton restart;

    public TerminateFrame(Integer totalScore)
    {
        initJFrame();

        initView(totalScore);

        this.setVisible(true);
    }

    private void initView(Integer totalScore) {
        //初始化布局
        JLabel scoreLabel = new JLabel();
        scoreLabel.setBounds(50,80,400,200);
        scoreLabel.setFont(new Font("微软雅黑",Font.PLAIN,28));
        scoreLabel.setText("总成绩："+ totalScore +"分");
        scoreLabel.setHorizontalAlignment(JLabel.CENTER);
        scoreLabel.setVerticalAlignment(JLabel.CENTER);
        this.getContentPane().add(scoreLabel);

        restart = new JButton("重新开始");
        restart.setBounds(200,400,100,40);
        restart.addActionListener(this);
        this.getContentPane().add(restart);

        exit = new JButton("退出考试");
        exit.setBounds(200,500,100,40);
        exit.addActionListener(this);
        this.getContentPane().add(exit);
    }

    private void initJFrame() {
        this.setSize(500,750);
        this.setLayout(null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setTitle("Java机考 结算页");
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == exit)
        {
            System.exit(0);
        }
        else if(e.getSource() == restart)
        {
            this.dispose();
            new StartFrame();
        }
    }
}
